package cn.com.lei.du.controller;

import cn.com.lei.du.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Random;
import java.util.UUID;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/getUser")
    public User getUser(){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName("用户");
        log.info("eureka client user ----> {}",user);
        return user;
    }

    @GetMapping("/getUserByName")
    public User getUserByName(String name){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName(name);
        log.info("get user by name----> {}",user);
        return user;
    }

    @PostMapping("/postUserByName")
    public User postUserByName(String name){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName(name + " hello");
        log.info("post user by name----> {}",user);
        return user;
    }

    @PostMapping("/postUserRequestBody")
    public User postUserRequestBody(@RequestBody User user){
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName(user.getName() + " RequestBody");
        log.info("post user by requestBody----> {}",user);
        return user;
    }

    @GetMapping("/feign/getUser")
    public User feignUser(){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName("用户");
        log.info("feign user ----> {}",user);
        return user;
    }

    @GetMapping("/feign/getUserParam")
    public User feignUserParam(@RequestParam("name") String name){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName(name + " feign param");
        log.info("feign user param----> {}",user);
        return user;
    }

    @GetMapping("/feign/getUserHeader")
    public User feignUserHeader(@RequestHeader("name") String name){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName(name + " feign header");
        log.info("feign user header----> {}",user);
        return user;
    }

    @PostMapping("/feign/getUserBody")
    public User feignUserHeader(@RequestBody User user){
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 2);
        user.setName(user.getName()+ " feign header");
        log.info("feign user body----> {}",user);
        return user;
    }

    @GetMapping("/feign/getUserPath/{name}")
    public User feignUserPath(@PathVariable("name") String name){
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 2);
        user.setName(user.getName()+ " feign path");
        log.info("feign user path----> {}",user);
        return user;
    }

    @GetMapping("/feign/getUserRetry")
    public User getUserRetry() throws InterruptedException {
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName("用户");
        int sleepTime = new Random().nextInt(4000);
        log.info("feign user retry---->{}",sleepTime);
        Thread.sleep(sleepTime);
        return user;
    }

    @GetMapping("/feign/getUserHys")
    public User getUserHys() throws InterruptedException {
        User user = new User();
        user.setId(1L);
        user.setCreateTime(new Date());
        user.setXcode(UUID.randomUUID().toString().replace("-",""));
        user.setSex((byte) 1);
        user.setName("用户");
        int sleepTime = new Random().nextInt(4000);
        log.info("feign user retry---->{}",sleepTime);
        Thread.sleep(sleepTime);
        return user;
    }
}
